package com.attendance.controller;

import com.attendance.Utils.PaginationUtil;
import com.attendance.model.Result.Response;
import com.attendance.model.Stats;
import com.attendance.service.IStatsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by OPTIPLEX on 2016/11/29.
 */

@RestController
@RequestMapping("/stats")
public class StatsController {

    @Autowired
    private IStatsService statsService;

    @RequestMapping(value = "",method = RequestMethod.GET)
    public Response getAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int rows,
                           @RequestParam(defaultValue = "id") String sort, @RequestParam(defaultValue = "asc") String order,
                           @RequestParam(defaultValue = "-1") Integer deptId,
                           @RequestParam(defaultValue = "1990-01-01") Date startDate,
                           @RequestParam(defaultValue = "2990-01-01") Date endDate ){
        PaginationUtil.preset(page, rows, sort, order);
        Response response = new Response();
        if(startDate.after(endDate)){
            Date tmp = startDate;
            startDate = endDate;
            endDate = startDate;
        }
        List<Stats> list = new ArrayList<>();
        if(deptId == -1) {
            list = statsService.getStatsAll(startDate, endDate);
        }
        else {
            list = statsService.getStatsByDeptId(deptId,startDate, endDate);
        }
        response.putResult(list);
        System.out.println(startDate);
        System.out.println(endDate);

        return response;
    }

}
